package com.example.mqtt.controller;

import com.example.mqtt.config.MqttProperty;
import com.example.mqtt.service.PubService;
import com.example.mqtt.service.SubService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/mqtt")
public class MqttController {
    @Resource
    private PubService pubService;
    @Resource
    private SubService subService;
    @Resource
    private MqttProperty mqttProperty;

    @GetMapping(value = "/hello")
    public String sayHello() {
        String DEFAULT_TOPIC = mqttProperty.getDefaultTopic()[0];
        String msg = "hello mqtt-springboot";

        pubService.sendMessage(DEFAULT_TOPIC, msg);
        return "发送消息".concat(msg);
    }

    @GetMapping(value = "/send-msg")
    public String sendMsg(@RequestParam String topic, @RequestParam String msg) {
        pubService.sendMessage(topic, msg);
        return "发送消息".concat(msg);
    }

    @GetMapping(value = "/send-msg/qos")
    public String sendMsg(@RequestParam String topic, @RequestParam Integer qos, @RequestParam String msg) {
        pubService.sendMessage(topic, qos, msg);
        return "发送消息".concat(msg);
    }

    @GetMapping(value = "/subscribe")
    public String subscribeTopic(@RequestParam String topic) {
        return subService.subscribeTopic(topic);
    }

}
